<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="区域" prop="area">
                      <el-select v-model="formData.area" placeholder="请选择区域">
                            <el-option label="请选择字典生成" value="" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="小区名称" prop="communityName">
                      <el-input v-model="formData.communityName" placeholder="请输入小区名称" />
                    </el-form-item>
                    <el-form-item label="房源标题" prop="title">
                      <el-input v-model="formData.title" placeholder="请输入房源标题" />
                    </el-form-item>
                    <el-form-item label="房源来源" prop="source">
                      <el-select v-model="formData.source" placeholder="请选择房源来源">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.HOUSE_SOURCE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="房屋类型" prop="houseType">
                      <el-select v-model="formData.houseType" placeholder="请选择房屋类型">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.HOUSE_TYPE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="户型" prop="layout">
                      <el-input v-model="formData.layout" placeholder="请输入户型" />
                    </el-form-item>
                    <el-form-item label="房源价格" prop="price">
                      <el-input v-model="formData.price" placeholder="请输入房源价格" />
                    </el-form-item>
                    <el-form-item label="有无阳台（0表示无，1表示有）" prop="balcony">
                      <el-select v-model="formData.balcony" placeholder="请选择有无阳台（0表示无，1表示有）">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="厨房类型" prop="kitchenType">
                      <el-select v-model="formData.kitchenType" placeholder="请选择厨房类型">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.KITCHEN_TYPE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="卫生间类型" prop="bathroomType">
                      <el-select v-model="formData.bathroomType" placeholder="请选择卫生间类型">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.BATHROOM_TYPE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="朝向" prop="orientation">
                      <el-select v-model="formData.orientation" placeholder="请选择朝向">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.ORIENTATION)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="水电类型" prop="utilityType">
                      <el-select v-model="formData.utilityType" placeholder="请选择水电类型">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.WATER_ELECTRICITY_TYPE)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
                    <el-form-item label="房源所有人" prop="owner">
                      <el-input v-model="formData.owner" placeholder="请输入房源所有人" />
                    </el-form-item>
                    <el-form-item label="房源状态" prop="status">
                      <el-select v-model="formData.status" placeholder="请选择房源状态">
                            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INFRA_BOOLEAN_STRING)"
                                       :key="dict.value" :label="dict.label" :value="dict.value" />
                      </el-select>
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as HouseInfoApi from '@/api/system/house';
      export default {
    name: "HouseInfoForm",
    components: {
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            area: undefined,
                            communityName: undefined,
                            title: undefined,
                            source: undefined,
                            houseType: undefined,
                            layout: undefined,
                            price: undefined,
                            balcony: undefined,
                            kitchenType: undefined,
                            bathroomType: undefined,
                            orientation: undefined,
                            utilityType: undefined,
                            owner: undefined,
                            status: undefined,
        },
        // 表单校验
        formRules: {
                        area: [{ required: true, message: '区域不能为空', trigger: 'change' }],
                        communityName: [{ required: true, message: '小区名称不能为空', trigger: 'blur' }],
                        title: [{ required: true, message: '房源标题不能为空', trigger: 'blur' }],
                        source: [{ required: true, message: '房源来源不能为空', trigger: 'change' }],
                        houseType: [{ required: true, message: '房屋类型不能为空', trigger: 'change' }],
                        layout: [{ required: true, message: '户型不能为空', trigger: 'blur' }],
                        price: [{ required: true, message: '房源价格不能为空', trigger: 'blur' }],
                        balcony: [{ required: true, message: '有无阳台（0表示无，1表示有）不能为空', trigger: 'change' }],
                        kitchenType: [{ required: true, message: '厨房类型不能为空', trigger: 'change' }],
                        bathroomType: [{ required: true, message: '卫生间类型不能为空', trigger: 'change' }],
                        utilityType: [{ required: true, message: '水电类型不能为空', trigger: 'change' }],
                        owner: [{ required: true, message: '房源所有人不能为空', trigger: 'blur' }],
                        status: [{ required: true, message: '房源状态不能为空', trigger: 'change' }],
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await HouseInfoApi.getHouseInfo(id);
            this.formData = res.data;
            this.title = "修改房屋信息";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增房屋信息";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await HouseInfoApi.updateHouseInfo(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await HouseInfoApi.createHouseInfo(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            area: undefined,
                            communityName: undefined,
                            title: undefined,
                            source: undefined,
                            houseType: undefined,
                            layout: undefined,
                            price: undefined,
                            balcony: undefined,
                            kitchenType: undefined,
                            bathroomType: undefined,
                            orientation: undefined,
                            utilityType: undefined,
                            owner: undefined,
                            status: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
